﻿CREATE FUNCTION dbo.f_get_rate
	(
	@currencyName varchar(50),
	@date date
	)
RETURNS money
AS
	BEGIN
	
	DECLARE @CurrencyID int
	DECLARE @rate money
	
	SELECT @CurrencyID = ID FROM NSI_CURRENCY WHERE SHORT_NAME = @currencyName 
	
	SELECT @rate = SUM(RATE) FROM NSI_EXCHANGE_RATE where NSI_CURRENCY_ID = @CurrencyID and DATE = @date
	
	IF @rate IS NULL
	BEGIN
		SET @rate = 0;
	END
	
	IF @currencyName = 'RUB'
	BEGIN
		SET @rate = 1;
	END
	RETURN @rate
	
	END
